<?php   
class ControllerMyplancommonHeader extends Controller {
	protected function index() {
	
		if(!$this->user->isLogged()){
			$this->user->logout();
			$this->redirect($this->url->link("myplan/login"));
		}
		
		$this->data['messageaction']=$this->url->link('page/message');
		$this->data["home"]=$this->url->link("myplan/myplanhome");
		$this->data['logout']=$this->url->link("myplan/myplanhome/logout");

		$this->data['title'] = 'Myplan';
		$this->data["account"]=$this->user->getAccount();
		
		
		$this->load->model('myplan/user');
		$newscount=$this->model_myplan_user->getNewsCount($this->user->getId());
		//$this->document->getTitle();
		
		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			$this->data['base'] = $this->config->get('config_ssl');
		} else {
			$this->data['base'] = $this->config->get('config_url');
		}
		
		$this->data['description'] = $this->document->getDescription();
		$this->data['keywords'] = $this->document->getKeywords();
		$this->data['links'] = $this->document->getLinks();	 
		$this->data['styles'] = $this->document->getStyles();
		$this->data['scripts'] = $this->document->getScripts();
		
		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			$server = HTTPS_IMAGE;
		} else {
			$server = HTTP_IMAGE;
		}	
				
		if ($this->config->get('config_icon') && file_exists(DIR_IMAGE . $this->config->get('config_icon'))) {
			$this->data['icon'] = $server . $this->config->get('config_icon');
		} else {
			$this->data['icon'] = '';
		}
		
		$this->data['name'] = $this->config->get('config_name');
				
		if ($this->config->get('config_logo') && file_exists(DIR_IMAGE . $this->config->get('config_logo'))) {
			$this->data['logo'] = $server . $this->config->get('config_logo');
		} else {
			$this->data['logo'] = '';
		}
		
		$this->data['action'] = $this->url->link('common/home');

		if (!isset($this->request->get['route'])) {
			$this->data['redirect'] = $this->url->link('common/home');
		} else {
			$data = $this->request->get;
			
			unset($data['_route_']);
			
			$route = $data['route'];
			
			unset($data['route']);
			
			$url = '';
			
			if ($data) {
				$url = '&' . urldecode(http_build_query($data));
			}			
			
			$this->data['redirect'] = $this->url->link($route, $url);
		}

    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && isset($this->request->post['language_code'])) {
			$this->session->data['language'] = $this->request->post['language_code'];
		
			if (isset($this->request->post['redirect'])) {
				$this->redirect($this->request->post['redirect']);
			} else {
				$this->redirect($this->url->link('common/home'));
			}
    	}		
		
		$this->data['language_code'] = $this->session->data['language'];
		

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		if (isset($this->session->data['warning'])) {
			$this->data['warning'] = $this->session->data['warning'];
			
			unset($this->session->data['warning']);
		} else {
			$this->data['warning'] = '';
		}
		
		$this->data['languages'] = array();

		
		
		foreach ($results as $result) {
			if ($result['status']) {
				$this->data['languages'][] = array(
					'name'  => $result['name'],
					'code'  => $result['code'],
					'image' => $result['image']
				);	
			}
		}

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && isset($this->request->post['currency_code'])) {
      		$this->currency->set($this->request->post['currency_code']);
			
			unset($this->session->data['shipping_methods']);
			unset($this->session->data['shipping_method']);
				
			if (isset($this->request->post['redirect'])) {
				$this->redirect($this->request->post['redirect']);
			} else {
				$this->redirect($this->url->link('common/home'));
			}
   		}
		
		// Menu
		$this->load->model('myplan/common');
		$this->data['categories'] = array();
					
		$categories = $this->model_myplan_common->getCategories();
		
		foreach ($categories as $category) {
			if($newscount!=0){
				if($category['page_path']=="information"){
					$category['name']=$category['name']."(".$newscount.")";
				}
			}
		
			if ($category['top']) {
				$children_data = array();
				
				$children = $this->model_myplan_common->getChildren($category['page_id']);
				
				foreach ($children as $child) {
					$data = array(
						'filter_page_id'  => $child['page_id'],
						'filter_sub_category' => true	
					);		
						
					$children_data[] = array(
						'name'  => $child['name'] . ' ()',
						'href'  => $this->url->link('product/category', 'path=' . $category['page_id'] . '_' . $child['page_id'])	
					);					
				}
				
				if($category['page_id']=='1'){
				// Level 1
				$this->data['categories'][] = array(
					'name'     => $category['name'],
					'children' => $children_data,
					'column'   => $category['column'] ? $category['column'] : 1,
					'href'     => $this->url->link('myplan/'.$category['page_path'])
				);
				}else{
					// Level 1
				$this->data['categories'][] = array(
					'name'     => $category['name'],
					'children' => $children_data,
					'column'   => $category['column'] ? $category['column'] : 1,
					'href'     => $this->url->link('page/'.$category['page_path'])
				);
				}
				
			}
			
		}

		
				
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/myplancommon/header.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/myplancommon/header.tpl';
		} else {
			$this->template = 'default/template/myplancommon/header.tpl';
		}
		
    	$this->render();
	} 	
}
?>
